package top.wuyang.trywork.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.wuyang.trywork.base.PageBean;
import top.wuyang.trywork.base.R;
import top.wuyang.trywork.dao.TbBillboardMapper;
import top.wuyang.trywork.entity.TbBillboard;
import top.wuyang.trywork.service.TbBillboardService;
import top.wuyang.trywork.vo.PageVO;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbBillboardServiceImpl implements TbBillboardService {
    private final TbBillboardMapper tbBillboardMapper;

    @Override
    public R<String> insert(TbBillboard tbBillboard) {
        int i = tbBillboardMapper.insert(tbBillboard);
        return i == 1 ? R.ok("新增成功", null) : R.fail("新增失败", null);
    }

    @Override
    public R<PageVO<TbBillboard>> queryPage(PageBean pageBean, TbBillboard tbBillboard){
        IPage<TbBillboard> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        QueryWrapper<TbBillboard> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(tbBillboard.getUsername()),"username",tbBillboard.getUsername());
        wrapper.like(StringUtils.hasText(tbBillboard.getTitle()),"title",tbBillboard.getTitle());
        wrapper.like(StringUtils.hasText(tbBillboard.getContent()),"content",tbBillboard.getContent());
        page = tbBillboardMapper.selectPage(page, wrapper);
        return R.ok(page);
    }

    @Override
    public R<TbBillboard> queryById(Integer id) {
        TbBillboard tbBillboard = tbBillboardMapper.selectById(id);
        return tbBillboard != null ? R.ok(tbBillboard) : R.fail("查询失败", null);
    }

    @Override
    public R<String> delete(Integer id){
        int i = tbBillboardMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
    }

    @Override
    public R<String> update(TbBillboard tbBillboard){
        int i = tbBillboardMapper.updateById(tbBillboard);
        return i == 1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
    }



}
